package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Area;
import com.mt.roll.core.dal.service.AreaServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.AreaVoMapper;
import com.mt.roll.wms.params.AreaParam;
import com.mt.roll.wms.params.AreaQueryParam;
import com.mt.roll.wms.service.AreaService;
import com.mt.roll.wms.vo.AreaVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther yanyang
 * @date 2021/10/15 10:16
 */

@Service
@Transactional
public class AreaServiceImpl extends BaseService implements AreaService {

    @Autowired
    private AreaServiceBiz areaServiceBiz;

    @Autowired
    private AreaVoMapper areaVoMapper;

    
    @Override
    public R<PageVo<AreaVo>> area(AreaQueryParam areaQueryParam) {
        IPage<AreaVo> page = areaVoMapper.area(new Page<>(areaQueryParam.getCurrent(),areaQueryParam.getSize()), areaQueryParam);
        return successful(new PageVo<>(page, AreaVo.class));
    }

    @Override
    public R<AreaVo> areaById(IdParam idParam) {
        Area area = areaServiceBiz.getById(idParam.getId());
        Assert.notNull("详情不存在", area);
        AreaVo areaVo = areaVoMapper.areaById(idParam.getId());
        return successful(areaVo);
    }

    @Override
    public R<IdVo> add(AreaParam areaParam) {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq(Area.CODE, areaParam.getCode());
        Assert.eqZero(areaServiceBiz.count(wrapper), "该缓存区已经存在，请勿重复添加");
        Area area = new Area();
        BeanUtils.copyProperties(areaParam, area);
        setCommonField(area);
        areaServiceBiz.save(area);
        return successful(IdVo.builder().id(area.getId()).build());
    }

    @Override
    public R<IdVo> update(AreaParam areaParam) {
        Area area = areaServiceBiz.getById(areaParam.getId());
        //修改信息需要检验是否已存在
        if (!area.getCode().equals(areaParam.getCode())) {
            QueryWrapper<Area> wrapper = new QueryWrapper<>();
            wrapper.eq(area.CODE, areaParam.getCode());
            Assert.eqZero(areaServiceBiz.count(wrapper),"该缓存区已经存在");
        }
        Area updateArea = new Area();
        BeanUtils.copyProperties(areaParam, updateArea);
        setUpdateCommonField(updateArea);
        areaServiceBiz.updateById(updateArea);
        return successful(IdVo.builder().id(area.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        Area area = areaServiceBiz.getById(idParam.getId());
        if (area == null) {
            return failed("缓存区不存在");
        }
        /*
        Area updateAlarm = new Area().setId(area.getId());
        updateAlarm.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateAlarm);
        areaServiceBiz.updateById(updateAlarm);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        */
        //areaServiceBiz.removeById(updateAlarm.getId());
        areaServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(area.getId()).build());
    }

    @Override
    public R<String> getCode() {
        return successful(CodeGeneratorHelper.getAreaCode());
    }

    
}
